package org.sanm.domain.project;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.sanm.common.Result;
import org.sanm.domain.company.Company;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/project")
@Tag(name = "项目管理", description = "项目管理")
public class ProjectController {

    @PostMapping("add")
    @Operation(summary = "添加项目", description = "添加项目")
    public Result<Project> add(@RequestBody Project project){
//        project.setUpdateTime(LocalDateTime.now());
//        project.setCreateTime(LocalDateTime.now());
        Db.save(project);
        return Result.success(project);
    }

    @PutMapping("update")
    @Operation(summary = "修改项目", description = "修改项目")
    public Result update(@RequestBody Project project){
        Db.updateById(project);
        return Result.success("修改成功");
    }

    @DeleteMapping("delete/{id}")
    @Operation(summary = "删除项目", description = "删除项目")
    public Result delete(@PathVariable("id") Integer id){
        Db.removeById(id, Project.class);
        return Result.success("删除成功");
    }

    @GetMapping("detail/{id}")
    @Operation(summary = "根据id查询项目", description = "根据id查询项目")
    public Result<Project> getDetail(@PathVariable("id") Integer id){
        Project project = Db.getById(id, Project.class);
        return Result.success(project);
    }

    @GetMapping("ids")
    @Operation(summary = "根据多个id查询项目", description = "根据多个id查询项目")
    public Result<List<Project>> getByIds(@RequestParam("ids") List<Integer> ids){
        List<Project> list = Db.lambdaQuery(Project.class).in(Project::getId, ids).list();
        return Result.success(list);
    }

    @GetMapping("list")
    @Operation(summary = "分页查询项目", description = "分页查询项目")
    public Result<Page<Project>> page(
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "provinceCode", required = false) String provinceCode,
            @RequestParam(value = "cityCode", required = false) String cityCode,
            @RequestParam(value = "areaCode", required = false) String areaCode,
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate
    ){
        Page<Project> page = Page.of(pageNum, pageSize);
        Db.lambdaQuery(Project.class)
                .like(name != null, Project::getName, name)
                .eq(provinceCode != null, Project::getProvinceCode, provinceCode)
                .eq(cityCode!= null, Project::getCityCode, cityCode)
                .eq(areaCode != null, Project::getAreaCode, areaCode)
                .between(startDate != null, Project::getCreateTime, startDate, endDate)
                .orderByDesc(Project::getCreateTime)
                .page(page);

        return Result.success(page);
    }
}
